󲿷÷ֱӲο MFC(C++)ļµĴ롣

˵һأϴĲ裺

#include "DLBT.hpp"
#pragma comment(lib, "DLBT.lib")

һʱʼBTʾ

void __fastcall TformMain::BTStartup()
{
	// BTģ
	WCHAR path[260];

    // ӦóICFǽ⣬ⱻûѡ"ֹó"
	HMODULE hModule = GetModuleHandle (NULL);
	if (hModule != NULL && GetModuleFileNameW(hModule, path, 260) != 0)
	{
		DLBT_AddAppToWindowsXPFirewall (path, L"BTںʾ");
	}

    // ǷҪ޸ϵͳĲ
    DWORD curLimit = DLBT_GetCurrentXPLimit ();
    if (curLimit != 0 && curLimit < 256)
	{
		if (DLBT_ChangeXPConnectionLimit (256))
		{
			//ʾûѾ޸
			/*
			CString str;
			str.Format (_T ("BTDLBTںˣ⵽ϵͳϵҪŻΪ˸õЧ\r\n BTԶϵͳŻҪԺЧЧãֹԺʹã\r\n \r\n ԭΪ%dڸΪ256"), curLimit);
			MessageBox (str, _T ("BTԶŻ"), MB_OK);
			*/
        }
    }

	// BTں˺DHT磬ÿͻ˱ǣֻTrackerʱʹã
	DLBT_KERNEL_START_PARAM param;
        param.startPort = 8010;    //󶨶˿ڣ8010ʼԣ8010ûռãʹ8010һೢԵendPortָķΧ
        param.endPort = 8030;
	DLBT_Startup (param);
	DLBT_DHT_Start ();	
}

һtorrentأϴ

       // Ŀǰdemoֻʾһ
	if (m_hDownloader != NULL)
	{
	   return;
	}
	m_hDownloader =  DLBT_Downloader_Initialize(L"D:\\Test\\dlbt.torrent", L"D:\\Test\\");

timerԻȡٶȡȵϢ


void __fastcall TformMain::TimerBTStatusTimer(TObject *Sender)
{
		HANDLE hDownloader = m_hDownloader;
		if (hDownloader == NULL) {
			return;
		}
	//TODO: ʾдʽжֵĳ̶ȣ̬ռ
        int nameLen = 1024;
		WCHAR name[1025] = {0};
		std::wstring str;
		if (DLBT_Downloader_GetTorrentName (hDownloader, name, &nameLen) == S_OK)
		{
			str = L"ļ:";
			str += name;
		}

		DLBT_DOWNLOAD_STATE state = DLBT_Downloader_GetState (hDownloader);
		str += L" ״̬";
        switch (state)
        {
        case BTDS_QUEUED:
			str += L"ʼ";
			break;
		case BTDS_CHECKING_FILES:
			str += L"ļ";
			break;
		case BTDS_DOWNLOADING_TORRENT:
			str += L"ȡ";
			break;
		case BTDS_DOWNLOADING:
			str += L"";
			break;
		case BTDS_PAUSED:
			str += L"ͣ";
            break;
		case BTDS_FINISHED:
			str += L"";
            break;
		case BTDS_SEEDING:
			str += L"";
            break;
		case BTDS_ALLOCATING:
			str += L"洢ռ";
            break;
		case BTDS_ERROR:
			str += L"";
			/*CStringA errA = DLBT_Downloader_GetLastError (hDownloader);
			CString err = A2T ((LPSTR)(LPCSTR)errA);
			SetItemText (i, 1, CString (_T (": ")) + err);*/
            break;
        }

		UINT64 uSize = DLBT_Downloader_GetTotalFileSize (hDownloader);
		if (state == BTDS_DOWNLOADING || state == BTDS_CHECKING_FILES || state == BTDS_FINISHED
			|| state == BTDS_SEEDING || state == BTDS_PAUSED)
        {
            UINT64 uDone = DLBT_Downloader_GetDownloadedBytes (hDownloader);
            float val;
			TCHAR szDim [10];
			float progress;
			//BytesToDisplayBytes (uDone, &val, szDim);
		    if (uSize != _UI64_MAX)
				progress = DLBT_Downloader_GetProgress (hDownloader);
		    else
				progress = 0.0f;
			//SetItemText (i, 3, str);


			UINT64 uLeft = uSize - uDone;
			UINT uSpeed = DLBT_Downloader_GetDownloadSpeed (hDownloader);

			/*
			if (uLeft == _UI64_MAX || uSpeed == 0)
				SetItemText (i, 4, _T (""));
			else
				SetItemText (i, 4, TimeInSecondToStr (UINT (uLeft / uSpeed)));
				*/
        }
		else
		{
			UINT uSpeed = 0;
			uSpeed = DLBT_Downloader_GetDownloadSpeed (hDownloader);
		}


		// Ϣ
		std::string aStr;
		char infoHash [256];
		int nLen = sizeof (infoHash) / sizeof (infoHash[0]);
		if (DLBT_Downloader_GetInfoHash (hDownloader, infoHash, &nLen) == S_OK)
			aStr = infoHash;

		int pieceCount = DLBT_Downloader_GetPieceCount (hDownloader);
		int pieceSize =  DLBT_Downloader_GetPieceSize (hDownloader);
		UINT upSpeed = DLBT_Downloader_GetUploadSpeed (hDownloader);
		UINT64 uBytes = DLBT_Downloader_GetUploadedBytes (hDownloader);
		float shareRate = (float)DLBT_Downloader_GetShareRate (hDownloader);

		int connectedPeers = 0, totalSeeds = 0, seedsConnected = 0, inCompleteCount = 0, curTotalSeeds = 0, curTotalPeers = 0;
		DLBT_Downloader_GetPeerNums (hDownloader, &connectedPeers, &totalSeeds, &seedsConnected, &inCompleteCount, &curTotalSeeds, &curTotalPeers);

		KERNEL_INFO info;
		DLBT_GetKernelInfo (&info);

		//ļϢ
		int fileCount = DLBT_Downloader_GetFileCount (hDownloader);
		for (int i = 0; i < fileCount; i ++)
		{
			//ʾpadding_file
			if (DLBT_Downloader_IsPadFile (hDownloader, i))
				continue;

			// ʾֻʾ10ļ
			if (i > 10)
			{
				break;
			}

			WCHAR fileName[MAX_PATH];
			int len = MAX_PATH;
			if (DLBT_Downloader_GetFilePathName (hDownloader, i, fileName, &len) != S_OK)
				continue;

			std::wstring strFileName = fileName;
			UINT64 fileSize = DLBT_Downloader_GetFileSize (hDownloader, i);

			// **** ע⣺ ȡļȵĲܻϵͳԴ˷ѣڱҪʱʹ ****
			float  filePercent = DLBT_Downloader_GetFileProgress (hDownloader, i);

			// ʱbUpdateExtÿļhashҪٶԱȸ£ʵҪ
			char infoHash [48] = {0};
			std::string fileHash;
			int nLen = sizeof (infoHash) / sizeof (infoHash[0]);
			if (DLBT_Downloader_GetFileHash (hDownloader, i, infoHash, &nLen) == S_OK)
				fileHash = infoHash;
		}

		//Ϣ

		PEER_INFO * pInfo = NULL;
		DLBT_GetDownloaderPeerInfoList (hDownloader, &pInfo);
		if (pInfo != NULL)
		{
			for (int i = 0; i < pInfo->count; i++)
			{
				std::string ip = pInfo->entries[i].ip;
				std::string client = pInfo->entries[i].client;
				std::string strType;

				if (pInfo->entries[i].connectionType == 1)
					strType = "P2SP";
				else if (pInfo->entries[i].connectionType == 2)
					strType = "UDPֱ͸";
				else
					strType = "׼Э";


				UINT64 downBytes =pInfo->entries[i].downloadedBytes;
				UINT downSpeed = pInfo->entries[i].downloadSpeed;

				UINT64 upBytes = pInfo->entries[i].uploadedBytes;
				UINT upSpeed = pInfo->entries[i].uploadSpeed;
			}

			DLBT_FreeDownloaderPeerInfoList (pInfo);
		}
}


